package ro.ubbcluj.cs.validation.framework.tags;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ro.ubbcluj.cs.validation.framework.tags.utils.TagUtils;

public class FormTag extends BaseTag {

	private static final long serialVersionUID = 2736494978695056883L;
	private Logger log = LoggerFactory.getLogger(FormTag.class);
	
	private static final String VALIDATION_SERVLET_MAPPING = "/validation";

	/**
	 * URL - Specifies where to send the form-data when a form is submitted
	 * (required)
	 */
	private String action;

	/**
	 * MIME_type - Specifies the types of files that can be submitted through a
	 * file upload
	 */
	private String accept;
	/**
	 * charset - Specifies the character-sets the server can handle for
	 * form-data
	 */
	private String acceptCharset;

	/**
	 * Specifies how form-data should be encoded before sending it to a server
	 * application/x-www-form-urlencoded multipart/form-data text/plain
	 */
	private String enctype;

	/**
	 * get/post - Specifies how to send form-data
	 */
	private String method;

	/**
	 * name - Specifies the name for a form
	 */
	private String name;

	/**
	 * Deprecated. Specifies where to open the action URL _blank _self _parent
	 * _top framename
	 */
	private String target;
	
	/**
	 * script - Script to be run when a form is submitted
	 */	
	private String onsubmit;
	
	/**
	 * script - Script to be run when a form is reset
	 */
	private String onreset;

	@Override
	public int doStartTag() throws JspException {
		if (StringUtils.isEmpty(action)) {
			log.error("The form tag 'action' attribute is empty");
			return SKIP_BODY;
		}
		try {
			pageContext.getOut().println("<form "+getAttributes()+getSpecificAttributes()+">");
		} catch (IOException e) {
			log.error("Error printing form tag: " + e.getMessage());
		}
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doAfterBody() throws JspException {
		try {
			HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
			String sourcePath = req.getRequestURI().replace(req.getContextPath(), "");
			pageContext.getOut().println("<input type=\"hidden\" name=\"sourcePageURI\" value=\""+sourcePath+"\" />");
			pageContext.getOut().println("<input type=\"hidden\" name=\"origActionURI\" value=\""+action+"\" />");
			pageContext.getOut().println("</form>");
		} catch (IOException e) {
			log.error("Error printing form tag: " + e.getMessage());
		}
		return SKIP_BODY;
	}
	
	@Override
	public int doEndTag() throws JspException {
		release();
		return EVAL_PAGE;
	}

	private String getSpecificAttributes(){
		StringBuilder attributes = new StringBuilder();
		attributes.append("action=\""+pageContext.getServletContext().getContextPath()+VALIDATION_SERVLET_MAPPING+"\" ");
		if (StringUtils.isNotEmpty(name)) {
			attributes.append("name=\""+TagUtils.normalizeName(name)+"\" ");
		}
		if (StringUtils.isNotEmpty(method)) {
			attributes.append("method=\""+method+"\" ");
		}
		if (StringUtils.isNotEmpty(enctype)) {
			attributes.append("enctype=\""+enctype+"\" ");
		}
		if (StringUtils.isNotEmpty(target)) {
			attributes.append("target=\""+target+"\" ");
		}
		if (StringUtils.isNotEmpty(accept)) {
			attributes.append("accept=\""+accept+"\" ");
		}
		if (StringUtils.isNotEmpty(acceptCharset)) {
			attributes.append("acceptCharset=\""+acceptCharset+"\" ");
		}
		if (StringUtils.isNotEmpty(onsubmit)) {
			attributes.append("onsubmit=\""+onsubmit+"\" ");
		}	
		if (StringUtils.isNotEmpty(onreset)) {
			attributes.append("onreset=\""+onreset+"\" ");
		}	
		return attributes.toString();
	}
	
	public void release() {
		super.release();
		this.action = null;
		this.accept = null;
		this.acceptCharset = null;
		this.enctype = null;
		this.method = null;
		this.name = null;
		this.target = null;
		this.onsubmit = null;
		this.onreset = null;
	}

	public final String getAction() {
		return action;
	}

	public final void setAction(String action) {
		this.action = action;
	}

	public final String getAccept() {
		return accept;
	}

	public final void setAccept(String accept) {
		this.accept = accept;
	}

	public final String getAcceptCharset() {
		return acceptCharset;
	}

	public final void setAcceptCharset(String acceptCharset) {
		this.acceptCharset = acceptCharset;
	}

	public final String getEnctype() {
		return enctype;
	}

	public final void setEnctype(String enctype) {
		this.enctype = enctype;
	}

	public final String getMethod() {
		return method;
	}

	public final void setMethod(String method) {
		this.method = method;
	}

	public final String getName() {
		return name;
	}

	public final void setName(String name) {
		this.name = name;
	}

	public final String getTarget() {
		return target;
	}

	public final void setTarget(String target) {
		this.target = target;
	}

	public final String getOnsubmit() {
		return onsubmit;
	}

	public final void setOnsubmit(String onsubmit) {
		this.onsubmit = onsubmit;
	}

	public final String getOnreset() {
		return onreset;
	}

	public final void setOnreset(String onreset) {
		this.onreset = onreset;
	}
}
